package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UpDownListener {

    @Autowired
    private IApArticleService articleService;

    @KafkaListener(topics = {"${topic.upDownTopic}"})
    public void handleMessage(ConsumerRecord<String, String> record) {
        String value = record.value();
        System.out.println("接收到消息: " + value);
        // 转换称为map
        Map map = JSON.parseObject(value, Map.class);
        long articleId = (long) map.get("articleId");
        boolean is_down = (boolean) map.get("is_down");
        // 更新文章表
        LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId, articleId);
        update.set(ApArticle::getIsDown, is_down);
        articleService.update(update);
    }
}
